home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xpaint-2.1.1 / rw / readWriteSGI.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  3KB  |  127 lines

  1. /* +-------------------------------------------------------------------+ */
  2. /* | Copyright 1993, David Koblas (koblas@netcom.com)                  | */
  3. /* |                                                                   | */
  4. /* | Permission to use, copy, modify, and to distribute this software  | */
  5. /* | and its documentation for any purpose is hereby granted without   | */
  6. /* | fee, provided that the above copyright notice appear in all       | */
  7. /* | copies and that both that copyright notice and this permission    | */
  8. /* | notice appear in supporting documentation.  There is no           | */
  9. /* | representations about the suitability of this software for        | */
  10. /* | any purpose.  this software is provided "as is" without express   | */
  11. /* | or implied warranty.                                              | */
  12. /* |                                                                   | */
  13. /* +-------------------------------------------------------------------+ */
  14.  
  15. #include <stdio.h>
  16. #include <gl/image.h>
  17. #include <sys/fcntl.h>
  18. #include "image.h"
  19.  
  20. int WriteSGI(char *file, Image *image)
  21. {
  22.     IMAGE        *o;
  23.     short        *rbuf = NULL, *gbuf = NULL, *bbuf = NULL;
  24.     int        x, y;
  25.     unsigned char    *ip;
  26.     int         d = 3;
  27.  
  28.     if (image->isGrey)
  29.         d = 1;
  30.     if ((o = iopen(file,"w",RLE(1),3,image->width,image->height,d)) == NULL)
  31.         return 1;
  32.  
  33.     if (d == 1) {
  34.         rbuf = (short *)malloc(image->width * sizeof(short));
  35.     } else {
  36.         rbuf = (short *)malloc(image->width * sizeof(short));
  37.         gbuf = (short *)malloc(image->width * sizeof(short));
  38.         bbuf = (short *)malloc(image->width * sizeof(short));
  39.     }
  40.  
  41.     for (y = 0; y < image->height; y++) {
  42.         for (x = 0; x < image->width; x++) {
  43.             ip = ImagePixel(image, x, y);
  44.             rbuf[x] = ip[0];
  45.             if (d != 1) {
  46.                 gbuf[x] = ip[1];
  47.                 bbuf[x] = ip[2];
  48.             }
  49.         }
  50.         if (rbuf != NULL) putrow(o, rbuf, image->height - y - 1, 0);
  51.         if (gbuf != NULL) putrow(o, gbuf, image->height - y - 1, 1);
  52.         if (bbuf != NULL) putrow(o, bbuf, image->height - y - 1, 2);
  53.     }
  54.  
  55.     iclose(o);
  56.  
  57.     if (rbuf != NULL) free(rbuf);
  58.     if (gbuf != NULL) free(gbuf);
  59.     if (bbuf != NULL) free(bbuf);
  60.  
  61.     return 0;
  62. }
  63.  
  64. int TestSGI(char *file)
  65. {
  66.     int        f = open(file, O_RDONLY);
  67.     unsigned short    c;
  68.     int        ret = 0;
  69.  
  70.     if (f < 0)
  71.         return 0;
  72.  
  73.     ret = ((read(f, &c, 2) == 2) && c == IMAGIC);
  74.  
  75.     close(f);
  76.  
  77.     return ret;
  78. }
  79.  
  80. Image *ReadSGI(char *file)
  81. {
  82.     Image        *image;
  83.     IMAGE        *in;
  84.     short        *rbuf, *gbuf, *bbuf;
  85.     int        x, y;
  86.     unsigned char    *ip;
  87.  
  88.     if ((in = iopen(file, "r")) == 0)
  89.         return NULL;
  90.  
  91.     if (in->zsize == 1)
  92.         image = ImageNewGrey(in->xsize, in->ysize);
  93.     else
  94.         image = ImageNew(in->xsize, in->ysize);
  95.  
  96.     rbuf = (short*)malloc(in->xsize * sizeof(short));
  97.     gbuf = bbuf = rbuf;
  98.     if (in->zsize != 1) {
  99.         gbuf = (short*)malloc(in->xsize * sizeof(short));
  100.         bbuf = (short*)malloc(in->xsize * sizeof(short));
  101.     }
  102.  
  103.     ip = image->data;
  104.  
  105.     for (y = in->ysize - 1; y >= 0; y--) {
  106.         getrow(in, rbuf, y, 0);
  107.         if (gbuf != rbuf) getrow(in, gbuf, y, 1);
  108.         if (bbuf != rbuf) getrow(in, bbuf, y, 2);
  109.  
  110.         for (x = 0; x < in->xsize; x++) {
  111.             *ip++ = rbuf[x] & 0xff;
  112.             if (in->zsize != 1) {
  113.                 *ip++ = gbuf[x] & 0xff;
  114.                 *ip++ = bbuf[x] & 0xff;
  115.             }
  116.         }
  117.     }
  118.  
  119.     free(rbuf);
  120.     if (gbuf != rbuf) free(gbuf);
  121.     if (bbuf != rbuf) free(bbuf);
  122.  
  123.     iclose(in);
  124.  
  125.     return image;
  126. }
  127.